package com.cyt.work.divideclass.service.impl;

import com.cyt.base.model.dto.ReturnResult;
import com.cyt.base.util.StringUtils;
import com.cyt.base.util.BaseConstant;
import com.cyt.base.util.DictCode;
import com.cyt.base.util.ErrorCode;
import com.cyt.sys.model.po.SysUser;
import com.cyt.sys.service.impl.BaseServiceImpl;
import com.cyt.work.divideclass.dao.DivideclassGradeDao;
import com.cyt.work.divideclass.model.po.DivideclassGrade;
import com.cyt.work.divideclass.model.vo.DivideclassGradeVo;
import com.cyt.work.divideclass.service.DivideclassGradeService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

/**
 * 智能分班学生成绩-智能分班学生成绩Service层实现类
 *
 * @author caideyong
 * @date 2019-12-20 14:16:42
 */
@Service("divideclassGradeService")
public class DivideclassGradeServiceImpl extends BaseServiceImpl<DivideclassGrade> implements DivideclassGradeService {

    @Resource
    private DivideclassGradeDao divideclassGradeDao;

    @Autowired
    public void setBaseDao(DivideclassGradeDao divideclassGradeDao) {
        this.baseDao = divideclassGradeDao;
    }

    /**
     * 查询对象集合
     *
     * @param divideclassGradeVo 对象
     * @return List 对象集合
     * @author caideyong
     * @date 2019-12-20 14:16:42
     */
    @Override
    public List<DivideclassGrade> listExample(DivideclassGradeVo divideclassGradeVo) {
        Example example = new Example(DivideclassGrade.class);
        example.setOrderByClause(" total desc");
        Example.Criteria criteria = example.createCriteria();
        if(StringUtils.isNotEmpty(divideclassGradeVo.getDivideId())){
            criteria.andEqualTo("divideId", divideclassGradeVo.getDivideId());
        }
        return divideclassGradeDao.selectByExample(example);
    }

    /**
     * 分页查询
     *
     * @param divideclassGradeVo 接受参数对象
     * @param pageNum          分页数
     * @param pageSize         每页显示条数
     * @return ReturnResult
     * @author caideyong
     * @date 2019-12-20 14:16:42
     */
    @Override
    public ReturnResult page(DivideclassGradeVo divideclassGradeVo, int pageNum, int pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        PageHelper.orderBy(" total desc");
        List<DivideclassGrade> list = listExample(divideclassGradeVo);
        PageInfo<DivideclassGrade> page = new PageInfo<>(list);
        return new ReturnResult(page.getList(), page.getTotal(), page.isHasNextPage());
    }

    /**
     * 添加或更新
     *
     * @param divideclassGradeVo 参数接受对象
     * @param sysUser      当前用户
     * @return ReturnResult
     * @author caideyong
     * @date 2019-12-20 14:16:42
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public ReturnResult addOrUpdate(DivideclassGradeVo divideclassGradeVo, SysUser sysUser) {
        if (StringUtils.isBlank(divideclassGradeVo.getId())) {
            DivideclassGrade divideclassGrade = new DivideclassGrade(sysUser);
            divideclassGrade.setXh(divideclassGradeVo.getXh());
            divideclassGrade.setName(divideclassGradeVo.getName());
            divideclassGrade.setChinese(divideclassGradeVo.getChinese());
            divideclassGrade.setMath(divideclassGradeVo.getMath());
            divideclassGrade.setEnglish(divideclassGradeVo.getEnglish());
            divideclassGrade.setPolitical(divideclassGradeVo.getPolitical());
            divideclassGrade.setHistory(divideclassGradeVo.getHistory());
            divideclassGrade.setGeography(divideclassGradeVo.getGeography());
            divideclassGrade.setPhysical(divideclassGradeVo.getPhysical());
            divideclassGrade.setChemistry(divideclassGradeVo.getChemistry());
            divideclassGrade.setBiological(divideclassGradeVo.getBiological());
            divideclassGrade.setStuId(divideclassGradeVo.getStuId());
            divideclassGradeDao.insert(divideclassGrade);
            return new ReturnResult(BaseConstant.DATABASE_SUCCEED_INSERT);
        } else {
            DivideclassGrade divideclassGrade = divideclassGradeDao.selectByPrimaryKey(divideclassGradeVo.getId());
            divideclassGrade.setXh(divideclassGradeVo.getXh());
            divideclassGrade.setName(divideclassGradeVo.getName());
            divideclassGrade.setChinese(divideclassGradeVo.getChinese());
            divideclassGrade.setMath(divideclassGradeVo.getMath());
            divideclassGrade.setEnglish(divideclassGradeVo.getEnglish());
            divideclassGrade.setPolitical(divideclassGradeVo.getPolitical());
            divideclassGrade.setHistory(divideclassGradeVo.getHistory());
            divideclassGrade.setGeography(divideclassGradeVo.getGeography());
            divideclassGrade.setPhysical(divideclassGradeVo.getPhysical());
            divideclassGrade.setChemistry(divideclassGradeVo.getChemistry());
            divideclassGrade.setBiological(divideclassGradeVo.getBiological());
            divideclassGrade.setStuId(divideclassGradeVo.getStuId());
            divideclassGradeDao.updateByPrimaryKey(divideclassGrade);
            return new ReturnResult(BaseConstant.DATABASE_SUCCEED_UPDATE);
        }
    }

    /**
     * 根据条件逻辑删除
     *
     * @param divideclassGradeVo 参数接受对象
     * @param sysUser 当前用户
     * @return ReturnResult
     * @author caideyong
     * @date 2019-12-20 14:16:42
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public ReturnResult remove(DivideclassGradeVo divideclassGradeVo, SysUser sysUser) {
        //要修改的字段
        DivideclassGrade divideclassGrade = new DivideclassGrade();
        //筛选条件
        Example example = new Example(DivideclassGrade.class);
        Example.Criteria criteria = example.createCriteria();
        if (StringUtils.isNotBlank(divideclassGradeVo.getId())) {
            criteria.andIn("id", Arrays.asList(divideclassGradeVo.getId().split(",")));
        }
        criteria.andEqualTo("removeTag", DictCode.BASE_REMOVE_TAG_NORMAL);
        divideclassGradeDao.updateByExampleSelective(divideclassGrade, example);
        return new ReturnResult(ErrorCode.CODE_0, BaseConstant.DATABASE_SUCCEED_DEL);
    }

    /**
     * 根据条件物理删除
     *
     * @param divideclassGradeVo 参数接受对象
     * @param sysUser      当前用户
     * @return ReturnResult
     * @author caideyong
     * @date 2019-12-20 14:16:42
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public ReturnResult delete(DivideclassGradeVo divideclassGradeVo, SysUser sysUser) {
        //筛选条件
        Example example = new Example(DivideclassGrade.class);
        Example.Criteria criteria = example.createCriteria();
        if(StringUtils.isNotEmpty(divideclassGradeVo.getId())){
            criteria.andIn("id",Arrays.asList(divideclassGradeVo.getId().split(",")));
        }
        if (StringUtils.isNotBlank(divideclassGradeVo.getDivideId())) {
            criteria.andEqualTo("divideId", divideclassGradeVo.getDivideId());
        }
        divideclassGradeDao.deleteByExample(example);
        return new ReturnResult(ErrorCode.CODE_0, BaseConstant.DATABASE_SUCCEED_DEL);
    }

    @Override
    public void insertList(List<DivideclassGrade> divideclassGradeList) {
        divideclassGradeDao.insertList(divideclassGradeList);
    }
}